package com.itboye.mengwu.app.adapter;

import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.buy.ui.DapeigouBean;
import com.itboye.mengwu.ui.shop.DapPeiGouJiaRuGouWuCheDialog;
import com.itboye.mengwu.ui.shop.DaPaiGouRootBean;

public class DapeigouAdapter extends BaseAdapter {

	FragmentActivity context;
	DaPaiGouRootBean bean;

	public DapeigouAdapter(FragmentActivity context, DaPaiGouRootBean bean) {
		this.context = context;
		this.bean = bean;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return bean == null ? 0 : bean.getItems().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return bean == null ? 0 : bean.getItems().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_dapeigou, null);
			holder.txt_goodsName = (TextView) convertView
					.findViewById(R.id.txt_goodsName);
			holder.img_goods = (ImageView) convertView
					.findViewById(R.id.img_goods);
			holder.price = (TextView) convertView.findViewById(R.id.price);
			holder.old_price = (TextView) convertView
					.findViewById(R.id.old_price);
			holder.select = (LinearLayout) convertView
					.findViewById(R.id.select);
			holder.shuxing=(TextView) convertView.findViewById(R.id.shuxing);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.txt_goodsName.setText(bean.getItems().get(position).getName());
		XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
				+ bean.getItems().get(position).getMainImg(), holder.img_goods);
		
		// if (bean.getItems().get(position).getSkuInfo() != null)
		// if (bean.getItems().get(position).getSkuInfo().getIconUrl() != null)
		// XImageLoader.load(GlobalConfig.API_URL
		// + "/picture/index?id="
		// + bean.getItems().get(position).getSkuInfo()
		// .getIconUrl(), holder.img_goods);
		holder.price.setText("￥0");

		holder.old_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		holder.old_price.setText("￥0");
		holder.select.setTag(position);
		if (bean.getItems().get(position).getSkuInfo().getTemp() == null) {//单规格
			holder.shuxing.setText(bean.getItems().get(position).getSkuInfo().getSkuDesc());
			holder.shuxing.setTag(bean.getItems().get(position).getSkuInfo().getId());
		}
		holder.select.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
//				 ByAlert.alert(bean.getItems().get(position).getSkuInfo().getTemp()+"");
				if (bean.getItems().get(position).getSkuInfo() != null) {
//					DapPeiGouJiaRuGouWuCheDialog dapPeiGouJiaRuGouWuCheDialog = new DapPeiGouJiaRuGouWuCheDialog(
//							bean.getItems().get(position), false);
//					dapPeiGouJiaRuGouWuCheDialog.show(
//							context.getSupportFragmentManager(), "");
				} else {

				}
			}
		});
		return convertView;
	}

	public static class ViewHolder {
		TextView txt_goodsName;
		ImageView img_goods;
		TextView price,shuxing;
		TextView old_price;
		TextView guiGe;
		LinearLayout select;
	}
}
